package com.spzx.product.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.product.api.domain.*;
import com.spzx.product.api.domain.vo.ProductSkuBrandVo;
import com.spzx.product.api.domain.vo.SkuDetailVo;
import com.spzx.product.api.domain.vo.SkuSearchVo;
import com.spzx.product.api.domain.vo.SkuStockVo;
import com.spzx.product.mapper.ProductMapper;
import com.spzx.product.mapper.ProductSkuMapper;
import com.spzx.product.service.CategoryService;
import com.spzx.product.service.ProductDetailsService;
import com.spzx.product.service.ProductSkuService;
import com.spzx.product.service.SkuStockService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductSkuServiceImpl extends ServiceImpl<ProductSkuMapper, ProductSku>
        implements ProductSkuService {
    @Resource
    private CategoryService categoryService;
    @Resource
    private SkuStockService skuStockService;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private ProductDetailsService productDetailsService;

    @Override
    public ProductSkuBrandVo getSkuOrBrand() {
        List<Category> categoryList = categoryService.list(new LambdaQueryWrapper<Category>().eq(Category::getParentId, 0));
        List<ProductSku> productSkuList = baseMapper.listHotSku();

        ProductSkuBrandVo productSkuBrandVo = new ProductSkuBrandVo();
        productSkuBrandVo.setCategoryList(categoryList);
        productSkuBrandVo.setProductSkuList(productSkuList);
        return productSkuBrandVo;
    }

    @Override
    public List<ProductSku> getSkuList(SkuSearchVo skuSearchVo) {
        return baseMapper.getSkuList(skuSearchVo);
    }

    @Override
    public SkuDetailVo getSkuDetail(Long id) {
        ProductSku sku = getOne(new LambdaQueryWrapper<ProductSku>().eq(ProductSku::getId, id).eq(ProductSku::getStatus, 1));
        if (sku == null) return null;

        SkuPrice skuPrice = new SkuPrice();
        skuPrice.setSalePrice(sku.getSalePrice());
        skuPrice.setMarketPrice(sku.getMarketPrice());

        Long productId = sku.getProductId();

        ProductDetails productDetails = productDetailsService.getOne(new LambdaQueryWrapper<ProductDetails>().eq(ProductDetails::getProductId, productId));
        String detailsImageUrl = productDetails.getImageUrls() == null ? "" : productDetails.getImageUrls();
        List<String> detailsImageUrlList = Arrays.asList(detailsImageUrl.split(","));

        Product product = productMapper.selectOne(new LambdaQueryWrapper<Product>().eq(Product::getId, productId).eq(Product::getStatus, 1));

        String sliderUrls = product.getSliderUrls() == null ? "" : product.getSliderUrls();
        List<String> sliderUrlList = Arrays.asList(sliderUrls.split(","));
        product.setSliderUrls(null);

        String specValue = product.getSpecValue();
        Object parse = JSON.parse(specValue);
        product.setSpecValue(null);

        SkuStock skuStock = skuStockService.getOne(new LambdaQueryWrapper<SkuStock>().eq(SkuStock::getSkuId, id));
        SkuStockVo skuStockVo = new SkuStockVo();
        skuStockVo.setSkuId(id);
        skuStockVo.setAvailableNum(skuStock.getAvailableNum());
        skuStockVo.setSaleNum(skuStock.getSaleNum());

        Map<String, Long> skuSpecValueMap = new HashMap<>();
        List<ProductSku> skuList = list(new LambdaQueryWrapper<ProductSku>().eq(ProductSku::getProductId, productId).eq(ProductSku::getStatus, 1));
        skuList.forEach(item -> {
            skuSpecValueMap.put(item.getSkuSpec(), item.getId());
        });

        SkuDetailVo skuDetailVo = new SkuDetailVo();
        skuDetailVo.setProductSku(sku);
        skuDetailVo.setProduct(product);
        skuDetailVo.setSkuPrice(skuPrice);
        skuDetailVo.setSliderUrlList(sliderUrlList);
        skuDetailVo.setDetailsImageUrlList(detailsImageUrlList);
        skuDetailVo.setSpecValueList(parse);
        skuDetailVo.setSkuStockVo(skuStockVo);
        skuDetailVo.setSkuSpecValueMap(skuSpecValueMap);
        return skuDetailVo;
    }
}




